package com.arethusa;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import simpleorm.core.SConnection;
import simpleorm.core.SPreparedStatement;
import simpleorm.core.SResultSet;
import simpleorm.ui.SUIConstants;

import com.arethusa.officialdb.Official;
import com.arethusa.officialdb.OfficialBean;
import com.ibm.j2x.swing.util.TableUtilities;
import com.ibm.j2x.util.CollectionUtilities;

public class SelectOfficial extends JDialog implements SUIConstants,
		MouseListener {

	private static final long serialVersionUID = 1L;
	private String sortBy = "Achternaam";  //  @jve:decl-index=0:
	private Boolean sortAscending = new Boolean(true);
	private List<?> olist;
	private OfficialBean selectedOfficial;
	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	
	private class TableSorter extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = jTable.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 
            int column = jTable.convertColumnIndexToModel(viewColumn); 
            if(e.getClickCount() == 1 && column != -1) {
                sortAscending = new Boolean(!sortAscending.booleanValue());
                sortBy = jTable.getModel().getColumnName(column);
                fillTable(sortBy, sortAscending);
            }
         }
	}
	

	public void mouseClicked(MouseEvent e) {
		int n = e.getClickCount();
		if(n==1) return;
		int row = getJTable().getSelectedRow();
		selectedOfficial = ((OfficialBean)olist.get(row));
		this.setVisible(false);
	}

	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}


	private void fillTable(String sortBy, Boolean ascending) {
		List<OfficialBean> tmp = new ArrayList<OfficialBean>();
		/// Prepare and execute the query
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SPreparedStatement stmt = Official.meta.select(
				"1=1", sortBy+(ascending.booleanValue()?"":" DESC"));
		SResultSet res = stmt.execute();
		
		/// loop through the results
		while (res.hasNext()) {
			OfficialBean strt = new OfficialBean(((Official)res.getRecord()));
			tmp.add(strt);
		}
		if( ! hasBegun ) { SConnection.commit(); }
    	olist = CollectionUtilities.observableList(tmp);
		TableUtilities.setViewToModel("OfficialModel.xml", "SelectOfficialList", getJTable(), olist);
		TableColumn col1 = getJTable().getColumnModel().getColumn(0);
		col1.setPreferredWidth(30);
		TableColumn col2 = getJTable().getColumnModel().getColumn(1);
		col2.setPreferredWidth(250);
		TableColumn col3 = getJTable().getColumnModel().getColumn(2);
		col3.setPreferredWidth(30);
		TableColumn col4 = getJTable().getColumnModel().getColumn(3);
		col4.setPreferredWidth(30);
		TableColumn col5 = getJTable().getColumnModel().getColumn(4);
		col5.setPreferredWidth(100);
	}

	public OfficialBean getOfficial() {
		return selectedOfficial;
	}
	/**
	 * @param owner
	 */
	public SelectOfficial(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(515, 360);
		this.setModal(true);
		this.setTitle("Selecteer Echtgenoot");
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		fillTable(sortBy, sortAscending);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.NORTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
//			 Make column header click result in sorting ascending (shift-click is descending)
			jTable.setColumnSelectionAllowed(false);
	        TableSorter listSorter = new TableSorter();
			JTableHeader th = jTable.getTableHeader();
	        th.addMouseListener(listSorter);
	        jTable.addMouseListener(this);
		}
		return jTable;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
